<template>
  <div>
    <div class="gva-table-box">
      <div>
        <el-form :inline="true" :model="searchData">
          <el-form-item label="客户姓名">
            <el-input v-model.trim="searchData.name" placeholder="搜索条件"/>
          </el-form-item>
          <el-form-item label="级别" prop="level">
            <el-select v-model="searchData.level" placeholder="请选择客户级别">
              <el-option v-for="item in levelOptions"
                         :label="item" :value="item"/>
            </el-select>
          </el-form-item>
          <el-form-item label="所属团队">
            <el-select v-model="searchData.teamId" label="所属团队" filterable placeholder="请选择" @change="getList">
              <el-option label="查询所有" value=""/>
              <el-option v-for="item in options" :key="item.teamId" :label="item.teamName" :value="item.teamId"/>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="search" @click="getList">查询</el-button>
            <el-button icon="refresh" @click="clear">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="gva-btn-list">
        <el-button type="primary" icon="plus" @click="openAdd">新增客户</el-button>
      </div>
      <el-table :data="tableData" row-key="ID" v-loading="loading">
        <el-table-column align="left" label="客户编号" min-width="120" prop="customerId"/>
        <el-table-column align="left" label="姓名" min-width="115" prop="name"/>
        <el-table-column align="left" label="手机号" min-width="130" prop="phone"/>
        <el-table-column align="left" label="性别" min-width="70" prop="sex"/>
        <el-table-column align="left" label="生日" min-width="99" prop="birthday"/>
        <el-table-column align="left" label="服务团队" min-width="110" prop="teamName"/>
        <el-table-column align="left" label="地址" min-width="150" prop="address"/>
        <el-table-column align="left" label="级别" min-width="90" prop="level"/>
        <el-table-column label="操作" min-width="160" fixed="right">
          <template #default="scope">
            <el-button type="primary" link icon="edit" @click="openEdit(scope.row)">编辑</el-button>
            <el-popover v-model="scope.row.visible" placement="top" width="160">
              <p>确定要删除吗?</p>
              <div style="text-align: right; margin-top: 8px;">
                <el-button type="primary" link @click="scope.row.visible = false">取消</el-button>
                <el-button type="primary" @click="deleteFunc(scope.row)">确定</el-button>
              </div>
              <template #reference>
                <el-button type="danger" link icon="delete">删除</el-button>
              </template>
            </el-popover>
          </template>
        </el-table-column>
      </el-table>
      <div class="gva-pagination">
        <el-pagination :current-page="page" :page-size="pageSize" :page-sizes="[10, 30, 50, 100]"
                       :total="total" layout="total, sizes, prev, pager, next, jumper"
                       :hide-on-single-page="pageSize===10"
                       @current-change="handleCurrentChange" @size-change="handleSizeChange"/>
      </div>
    </div>
    <!--    新增修改模态框-->
    <el-dialog v-model="openDialog" :title="dialogFlag==='add'?'新增客户':'修改客户'" :show-close="false"
               :close-on-press-escape="false" :close-on-click-modal="false" draggable>
      <div style="height:40vh;overflow:auto;padding:0 12px;">
        <el-form ref="customerFrom" :model="customerInfo" label-width="80px">
          <el-form-item v-if="dialogFlag === 'edit'" label="账号" prop="userName">
            <el-input readonly="readonly" v-model.trim="customerInfo.customerId"/>
          </el-form-item>
          <el-form-item label="姓名" prop="name">
            <el-input v-model.trim="customerInfo.name"/>
          </el-form-item>
          <el-form-item label="手机号" prop="phone">
            <el-input v-model.trim="customerInfo.phone"/>
          </el-form-item>
          <el-form-item label="出生日期" prop="birthday">
            <el-date-picker
                v-model="customerInfo.birthday"
                type="date"
                placeholder="选择一个时间"
                value-format="YYYY-MM-DD"
            />
          </el-form-item>
          <el-form-item label="性别" prop="sex">
            <el-radio-group v-model.trim="customerInfo.sex" class="ml-4">
              <el-radio label="男" size="large">男</el-radio>
              <el-radio label="女" size="large">女</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="级别" prop="level">
            <el-select v-model="customerInfo.level" placeholder="请选择客户级别">
              <el-option v-for="item in levelOptions"
                         :label="item" :value="item"/>
            </el-select>
          </el-form-item>
          <el-form-item label="服务团队">
            <el-select v-model="customerInfo.teamId" filterable placeholder="请选择服务团队">
              <el-option v-for="item in options"
                         :key="item.teamId"
                         :label="item.teamId+':'+item.teamName"
                         :value="item.teamId"/>
            </el-select>
          </el-form-item>
          <el-form-item label="地址" prop="phone">
            <el-input v-model.trim="customerInfo.address"/>
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="closeDialog()">取 消</el-button>
          <el-button type="primary" @click="editOrAdd()">确 定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import {nextTick, ref} from 'vue'
import {createExaCustomer, deleteExaCustomer, getExaCustomerList, updateExaCustomer} from "@/api/customer";
import {ElMessage} from "element-plus";
import {employeesList} from "@/api/authority";
import {getTeamList} from "@/api/team";
import {setUserTeam} from "@/api/user";
//data
const tableData = ref()
const customerInfo = ref({
  customerId: '',
  name: '',
  phone: '',
  sex: '',
  birthday: '',
  teamId: '',
  address: '',
  level: ''
})
const customerFrom = ref()
const openDialog = ref(false)
const loading = ref(true)
const dialogFlag = ref()
//page
const total = ref(0)
const page = ref(1)
const pageSize = ref(10)
const searchData = ref({name: '', employeeId: '', level: '', teamId: ''})

//下拉选择框数据
const options = ref([])
const levelOptions = ref([
  'vip',
  'svip',
  '普通会员'
])

const clear = () => {
  searchData.value = {}
  getList()
}

// 分页
const handleSizeChange = (val) => {
  pageSize.value = val
  getList()
}
const handleCurrentChange = (val) => {
  page.value = val
  getList()
}
//查询
const getList = async () => {
  loading.value = true
  const res = await getExaCustomerList({page: page.value, pageSize: pageSize.value, ...searchData.value})
  await getTeam()
  if (res.code === 0) {
    res.data.records.forEach(e => {
      let teamName
      options.value.forEach(i => {
        if (e.teamId === i.teamId) {
          teamName = i.teamName
        }
      })
      e.teamName = teamName;
    })
    tableData.value = res.data.records
    total.value = +res.data.total
    loading.value = false
  }
}
//删除
const deleteFunc = async (row) => {
  const res = await deleteExaCustomer(row.customerId)
  if (res.code === 0) {
    ElMessage({type: 'success', message: res.msg})
  }
  row.visible = false
  await getList()
}
//修改
const openEdit = (row) => {
  customerInfo.value = JSON.parse(JSON.stringify(row))
  dialogFlag.value = 'edit'
  openDialog.value = true
}
//新增
const openAdd = () => {
  dialogFlag.value = 'add'
  openDialog.value = true
}

//新增或修改请求
const editOrAdd = async () => {
  let res
  const req = {...customerInfo.value}
  if (dialogFlag.value === 'add') {
    res = await createExaCustomer(req)
  } else {
    res = await updateExaCustomer(req)
  }
  if (res.code === 0) {
    ElMessage({type: 'success', message: res.msg})
    await closeDialog()
  }
}
//关闭模态框
const closeDialog = async () => {
  openDialog.value = false
  dialogFlag.value = ''
  initForm()
  await getList()
}

const changeTeam = async (row, flag) => {
  if (row.teamId === null || flag) {
    return
  }
  await nextTick()
  const res = await setCustomerTeam({
    customerId: row.customerId,
    teamId: row.teamId
  })
  if (res.code === 0) {
    ElMessage({type: 'success', message: res.msg})
  }
}

//初始化表单
const initForm = () => {
  if (customerFrom.value) {
    customerFrom.value.resetFields()
  }
  customerInfo.value = {
    customerId: '',
    name: '',
    phone: '',
    sex: '',
    birthday: '',
    employeeId: '',
    employeeName: '',
    address: '',
    level: ''
  }
}
//获取团队列表
const getTeam = async () => {
  const res = await getTeamList({page: 1, pageSize: 999})
  options.value = res.data.records
}
getList()

</script>
<style scoped lang="scss"></style>
